package us.icitap.cm.ui.shared;

import java.util.Arrays;
import java.util.Set;

import us.icitap.cm.business.AppListManadatoryValuesEnum;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.Person;
import us.icitap.cm.business.entities.PoliceOperation;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.business.entities.SexEnum;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTwinColSelect;
import us.icitap.cm.utils.ProjectLists;

import com.vaadin.data.Buffered.SourceException;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;

public class PoliceOperationField extends CustomField<PoliceOperation> {

	private final CheckBox poCheckBox = new CheckBox("Realizuar me 'Operacion Policor'");
	
	private CustComboBox poTypeSelect = new CustComboBox("Shtrirja");
	
	private CustTwinColSelect statesTwin = new CustTwinColSelect("Shtetet e perfshira"); 
	

	private final CustBeanFieldGroup<PoliceOperation> binder = new CustBeanFieldGroup<PoliceOperation>(PoliceOperation.class);
	
	
	@Override
	protected Component initContent() {
		final VerticalLayout vl = new VerticalLayout();
		
		vl.addComponent(poCheckBox);
		
		binder.bind(poTypeSelect, "operationType");
		binder.bind(statesTwin, "statesList");
		
		poTypeSelect.setWidth("200px");
		statesTwin.setWidth("450px");
		
		statesTwin.setContainerDataSource(new BeanItemContainer<States>(States.class, ProjectLists.getStatesList(false)));
		statesTwin.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		statesTwin.setItemCaptionPropertyId("tag");
		statesTwin.setConverter(Set.class);
		
		poCheckBox.setImmediate(true);
		
		poCheckBox.addValueChangeListener(new ValueChangeListener() {
			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				if(event.getProperty() != null){
					Boolean val = (Boolean)event.getProperty().getValue();
					if(val.booleanValue()){
						vl.addComponent(poTypeSelect);
						PoliceOperation policeOperation = new PoliceOperation();
						PoliceOperationField.this.setValue(policeOperation);
						
						
					}
					else{
						vl.removeComponent(poTypeSelect);	
						vl.removeComponent(statesTwin);
						PoliceOperationField.this.setValue(null);
					}
				
				}
				
			}
		});
		
		poTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.OperationType, false)));
		poTypeSelect.setNullSelectionAllowed(false);
		poTypeSelect.setImmediate(true);
		poTypeSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				if(event.getProperty() != null && event.getProperty().getValue() != null){
					AppList oType = (AppList) event.getProperty().getValue();
					
					
					if(oType.toString().equals(AppListManadatoryValuesEnum.OperationType_NDERKOMBETAR.toString()))
						vl.addComponent(statesTwin);	
					else{
						vl.removeComponent(statesTwin);
						statesTwin.setValue(null);
					}
						
				}
					
				
			}
		});
		
		
		
		
		return vl;
	}

	@Override
	public Class<? extends PoliceOperation> getType() {
		// TODO Auto-generated method stub
		return PoliceOperation.class;
	}

	@Override
	protected void setInternalValue(PoliceOperation newValue) {
		super.setInternalValue(newValue);
		if(newValue != null)
			binder.setItemDataSource(newValue);
	}

	@Override
	public void commit() throws SourceException, InvalidValueException {
		try {
		
			binder.commit();
		
		} catch (Exception e) {
			// TODO: handle exception
		}
		super.commit();
	}
	
	
}
